package com.order.service;

import com.order.service.impl.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.order.domain.TOrder;
import com.order.mapper.TOrderMapper;
import com.order.service.impl.TOrderService;

/**
 * @author cyt
 * @create 2020-06-07 10:33
 */
@Slf4j
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {
    @Autowired
    TOrderMapper tOrderMapper;
    @Autowired
    AccountService accountService;
    @Autowired
    StorageService storageService;

    /**
     * accountService
     *
     * @param tOrder
     * @return
     */
    //@GlobalTransactional(timeoutMills = 300000)
    @Override
    public TOrder createOrder(TOrder tOrder) {
        int userId = tOrder.getUserId();
        int count = tOrder.getCount();
        int productId = tOrder.getProductId();
        log.info("创建订单开始");
        // 减少库存
        storageService.decStorage(productId, count);
        // 扣除余额
        accountService.decAccount(userId, count);
        // 修改订单
        tOrder.setStatus(1);
        log.info("创建订单成功");
        tOrderMapper.insert(tOrder);
        return tOrder;
    }
}
